# p04_refine_lkt.sh
# - 描述: 精化地理编码表
# - 作者: iam002
# - 日期: 2025年2月24日
# - 版本: V1.0

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# 读取全局变量
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if [ ! -f "$1" ]; then
    source p00_global_params.sh
else
    source $1
fi

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# 日志路径
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
cur_script_name=$(basename "$0" .sh)
out_log_path=$in_output_folder/$cur_script_name".log"

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# 创建 diff_par 文件
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# 多视 SAR 影像
out_mli_path=$in_output_folder/$in_produce_name".mli"
# 多视SAR雷达参数
out_mli_par_path=$in_output_folder/$in_produce_name".mli_par"
# 雷达坐标系下的仿真SAR影像
out_simsar_rdc_path=$in_output_folder/$in_produce_name.simsar_rdc

mli_diff_par_path=$in_output_folder/$in_produce_name".diff_par"

if [ -f "$mli_diff_par_path" ]; then
    rm -f $mli_diff_par_path
fi
create_diff_par $out_mli_par_path - $mli_diff_par_path 1 0 | tee $out_log_path

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# 计算粗偏移量
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

# 程序接口说明:
# init_offsetm <MLI1> <MLI2> <DIFF_par> 
#   [rlks] [azlks] 
#   [rpos] [azpos] center of region for comparison in rg/az (enter - for default: image center)
#   [offr] [offaz] initial rg/az offset (enter - for default from DIFF_par)
#   [thres] 
#   [patch] 
#   [cflag] copy offsets to the range and azimuth offset polynomials in DIFF_par

echo -e "Running init_offsetm ...\n" | tee -a $out_log_path
init_offsetm $out_mli_path $out_simsar_rdc_path $mli_diff_par_path \
    1 1 \
    - - \
    - - \
    0.1 \
    512 \
    1 | tee -a $out_log_path
echo -e "\nFinish init_offsetm!" | tee -a $out_log_path

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# 分小块执行匹配
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
out_offs_path=$in_output_folder/$in_produce_name".offs"
out_ccp_path=$in_output_folder/$in_produce_name".ccp"
out_offsets_path=$in_output_folder/$in_produce_name".offsets"

# 程序接口说明:
# offset_pwrm <MLI1> <MLI2> <DIFF_par> 
#   <offs> 
#   <ccp> 
#   [rwin] [azwin] patch size (range pixels, enter - for default from offset parameter file)
#   [offsets] 
#   [n_ovr] 
#   [nr] [naz] number of offset estimates, enter - for default from offset parameter file
#   [thres] cross-correlation threshold
#   [lanczos] [bw_frac] [pflag] [pltflg] [ccs] [std_mean]

echo -e "Running offset_pwrm ...\n" | tee -a $out_log_path
offset_pwrm $out_mli_path $out_simsar_rdc_path $mli_diff_par_path \
    $out_offs_path \
    $out_ccp_path \
    512 512 \
    $out_offsets_path \
    - \
    - - \
    - | tee -a $out_log_path
echo -e "\nFinish offset_pwrm!" | tee -a $out_log_path

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# 拟合多项式
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
echo -e "Running offset_fitm ...\n" | tee -a $out_log_path
offset_fitm $out_offs_path $out_ccp_path $mli_diff_par_path - - - 3 0 | tee -a $out_log_path
echo -e "\nFinish offset_fitm!" | tee -a $out_log_path

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# 精化地理编码表
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

# 重采样雷达参数
out_resamp_dem_par_path=$in_output_folder/$in_produce_name"_resamp.dem_par"
dem_width=$(grep -oP '^width:\s*\K\d+' "$out_resamp_dem_par_path")

# 原始地理编码表
out_lkt_path=$in_output_folder/$in_produce_name.lkt

# 精化地理编码表
out_fine_lkt_path=$in_output_folder/$in_produce_name.fine_lkt

echo -e "Running gc_map_fine ...\n" | tee -a $out_log_path
gc_map_fine $out_lkt_path $dem_width $mli_diff_par_path $out_fine_lkt_path 0
echo -e "\nFinish gc_map_fine!" | tee -a $out_log_path

